package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id){
        int i = 10/0;
        String result = paymentHystrixService.paymentInfoOK(id);
        log.info("*******result:"+result);
        return result;
    }
    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2500") // 4000>3000 1500<3000
    })
    //@HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id") Integer id){
        int i =10/0;
        String result = paymentHystrixService.paymentInfoTimeout(id);
        log.info("*******result:"+result);
        return result;
    }
    //兜底方法 托底方法
    public String paymentInfoTimeoutHandler(Integer id){
        return "局部兜底方法，这是80 order端，访问出问题，请稍后再试";
    }


    //下面是全局fallback方法
    public String payment_Global_FallbackMethod(){
        return "全局兜底方法，Global异常处理信息，请稍后再试,(┬＿┬)";
    }

}
